package com.zd.jdbc;

import java.sql.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class CustomerDAOImpl implements CustomerDAO{

    @Override
    public void add(Customer customer) {

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/fast_food" , "root" , "000000");

            String sql = "insert into customer(id, create_time, update_time, phone_number, username) values(? , ? , ? , ? ,? )";

            // 预编译 效率也高 也更安全 防止sql注入 只要传参数给 sql 这个更好
            PreparedStatement preparedStatement = conn.prepareStatement(sql);

            preparedStatement.setLong(1, 20102);

            LocalDateTime createTime = LocalDateTime.of(2025, 6, 27, 10, 10, 10);
            LocalDateTime updateTime = LocalDateTime.of(2025, 6, 27, 10, 10, 10);

            preparedStatement.setTimestamp(2, Timestamp.valueOf(createTime));
            preparedStatement.setTimestamp(3, Timestamp.valueOf(updateTime));

            preparedStatement.setString(4, "13000000000");
            preparedStatement.setString(5, "孙悟空");

            preparedStatement.executeUpdate();

            preparedStatement.close();
            conn.close();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


    }

    @Override
    public void delete(Long id) {

    }

    @Override
    public void update(Customer customer) {

    }

    @Override
    public Customer load(Long id) {
        Customer customer = null;


        try {
            // 1 加载驱动
            Class.forName("com.mysql.jdbc.Driver");

            //Class.forName("com.mysql.cj.jdbc.Driver");

            // 2 从驱动获取数据的连接

            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/fast_food" , "root" , "root");

            // 3 从连接创建执行sql的工具
            Statement statement = conn.createStatement();

            String sql = "select * from customer where id="+id;

            ResultSet resultSet = statement.executeQuery(sql);

            // id最多有1条 if即可 while也行
            if (resultSet.next()){// cusor 游标 指针
                customer = new Customer();
                customer.setId(resultSet.getLong("id"));
                customer.setCreateTime(resultSet.getTimestamp("create_time").toLocalDateTime());
                customer.setUpdateTime(resultSet.getTimestamp("update_time").toLocalDateTime());
                customer.setUsername(resultSet.getString("username"));
                customer.setPhoneNumber(resultSet.getString("phone_number"));

            }

            resultSet.close();
            statement.close();
            conn.close();

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


        return customer;
    }

    @Override
    public List<Customer> list() {

        List<Customer> customers = new ArrayList<>();


        try {
            // 1 加载驱动
            Class.forName("com.mysql.jdbc.Driver");

            //Class.forName("com.mysql.cj.jdbc.Driver");

            // 2 从驱动获取数据的连接

            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/fast_food" , "root" , "root");

            // 3 从连接创建执行sql的工具
            Statement statement = conn.createStatement();

            String sql = "select * from customer";

            ResultSet resultSet = statement.executeQuery(sql);

            while (resultSet.next()){// cusor 游标 指针
                Customer  customer = new Customer();
                customer.setId(resultSet.getLong("id"));
                customer.setCreateTime(resultSet.getTimestamp("create_time").toLocalDateTime());
                customer.setUpdateTime(resultSet.getTimestamp("update_time").toLocalDateTime());
                customer.setUsername(resultSet.getString("username"));
                customer.setPhoneNumber(resultSet.getString("phone_number"));

                customers.add(customer);
            }


            resultSet.close();
            statement.close();
            conn.close();

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


        return customers;
    }
}
